package com.example.news;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.news.dao.LoginDao;

public class RegisterActivity extends AppCompatActivity {
    EditText etregisterusername=null;
    EditText etregisterpassword=null;
    Button btnregistersave=null;
    private LoginDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        etregisterusername=(EditText) findViewById(R.id.etregisteruser);
        etregisterpassword=(EditText)findViewById(R.id.etregisterpassword);
        btnregistersave=(Button)findViewById(R.id.btnregistersave);
        dao=new LoginDao(this);
        btnregistersave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    if((!etregisterusername.getText().toString().equals(""))&&(!etregisterpassword.getText().toString().equals(""))){
                        if(dao.register(etregisterusername.getText().toString(),etregisterpassword.getText().toString())){
                            Toast.makeText(RegisterActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();
                            etregisterusername.setText("");
                            etregisterpassword.setText("");
                        }
                    }else{
                        Toast.makeText(RegisterActivity.this,"用户名密码不能为空！",Toast.LENGTH_SHORT).show();
                        etregisterusername.setText("");
                        etregisterpassword.setText("");
                    }


                }catch (Exception e){
                    e.printStackTrace();
                    Toast.makeText(RegisterActivity.this,"注册失败！",Toast.LENGTH_SHORT).show();
                }

            }
        });

    }
}
